
let cfg = require('../utils/cfg');
let Node = require('../node');

class Action{
    constructor(){
        this._id = cfg.genId();
        this._target = null;
        this._duration = 0;
        this._bRuning = false;
    }

    get running(){
        return this._bRuning;
    }

    set duration(d){
        this._duration = d;
    }

    get duration(){
        return this._duration;
    }

    start(){
        this._elapsed = 0;
        this._bRuning = true;
    }

    isDone(){
        return this._elapsed >= this._duration;
    }

    get id(){
        return this._id;
    }

    set target(newTarget){

        // 为了删除 action 的时候,避免内存泄漏
        if(!newTarget){
            this._target = null;
            return;
        }

        if(!(newTarget instanceof Node)){
            throw new Error(`newTarget:${newTarget} is not a node!`);
        }
        this._target = newTarget;
    }

    get target(){
        return this._target;
    }

    step(dt){
        this._elapsed += dt;

        let t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
        t = (1 > t ? t : 1);
        this.update(t > 0 ? t : 0);
    }

    update(t){}  // eslint-disable-line
}

module.exports = Action;
